package situ.autoweter.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import situ.autoweter.dao.RdReadDao;
import situ.autoweter.dao.RdYearmonthDao;
import situ.autoweter.entity.RdRead;
import situ.autoweter.service.RdYearmonthService;
import situ.autoweter.entity.RdYearmonth;

import java.text.SimpleDateFormat;
import java.util.*;
/**
 * RdYearmonth的路由接口服务
 * 
 * @author 胡文
 *
 */
@Controller
public class RdYearmonthController {

	/** RdYearmonthService服务 */
	@Autowired
	private RdYearmonthService rdYearmonthService;
	
	/**
	 * 查询所有RdYearmonth数据的方法
	 * @param value
	 * @return
	 */
	@Autowired
	RdYearmonthDao dao;
	@RequestMapping(value = "RdYearmonth.html")
	public String find(RdYearmonth value,Map map) {
		List<Map> rdYearmonths = dao.queryALL();
		map.put("list",rdYearmonths);
         return "RdYearmonth.html";
	}
	
	/**
	 * 通过id查询RdYearmonth数据的方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "queryRdYearmonthById/{id}", produces = {"application/json;charset=UTF-8"})
	public String findOne(@PathVariable(name="id") Object id,Map map) {
        RdYearmonth obj=  rdYearmonthService.findOne(id);
        map.put("RdYearmonth",obj);
        return "页面名称自己改";
	}
	
	/**
	 * 插入RdYearmonth属性不为空的数据方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "saveRdYearmonth", produces = {"application/json;charset=UTF-8"})
	public String save(RdYearmonth value) {
		  rdYearmonthService.saveNotNull(value);
        return "redirect:/queryRdYearmonth";
	}
	
	/**
	 * 更新RdYearmonth属性不为空的数据方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "updateRdYearmonth", produces = {"application/json;charset=UTF-8"})
	public String update(RdYearmonth value) {
		  rdYearmonthService.updateNotNullById(value);
          return "redirect:/queryRdYearmonth";
	}
	
	/**
	 * 通过id删除RdYearmonth数据方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "delRdYearmonthById/{id}", produces = {"application/json;charset=UTF-8"})
	public String delete(@PathVariable(name="id") Object id) {
		  rdYearmonthService.deleteById(id);
          return "redirect:/queryRdYearmonth";
	}



	@Autowired
	RdReadDao dao2;
	@RequestMapping("/NewRdYearmonth")
	public String NewRdYearmonth(Map map,RdYearmonth rdYearmonth){
		List<RdRead> rdReads = dao2.selectRdRead(null);
		Date date=new Date();
		SimpleDateFormat dateFormat=new SimpleDateFormat("YYYY-MM");
		String format = dateFormat.format(date);
		String[] split = format.split("-");


		for (RdRead rdRead:rdReads){
			if(rdRead.getReadMonth()==12){
				rdRead.setReadMonth(1);
				rdRead.setReadYear(rdRead.getReadYear()+1);

			}else {
				rdRead.setReadMonth(rdRead.getReadMonth()+1);
			}

			if (rdRead.getReadMonth()==Integer.parseInt(split[1])&&rdRead.getReadYear()==Integer.parseInt(split[0])){
				dao.NewRdYearmonth(rdRead);
			}


		}
		dao.updateEndDate();
        dao.up();
		rdYearmonth.setIsCurrent(1);


		rdYearmonth.setReadYear(Integer.parseInt(split[0]));
		rdYearmonth.setReadMonth(Integer.parseInt(split[1]));

		rdYearmonth.setInitDate(date.toInstant());

		rdYearmonth.setEndDate(null);
		rdYearmonth.setInitEmpID(1);
		rdYearmonthService.saveNotNull(rdYearmonth);



		return find(new RdYearmonth(),map);
	}
}
